<?

if(strpos($_SERVER['SCRIPT_URL'],'/trecord.html')===false && strpos($_SERVER['SCRIPT_URL'],'/accountTrecord.html')===false)
    exit;
echo $out_html;
$member_ids = array();
$admin_account =array('admin','123666');
$where =" where 1=1";
$admin_case = array(//和盟主能看到的一样
    'C' => array('平台','供应商','盟主','省代理商','市代理商','邀请人','分销商'),
    'T' => array('adminid','applyid','fid','agencyID_P','agencyID_C','inviterID','P_inviterID'),
    'M' => array('adminid_lirun','apply_lirun','fid_lirun','p_agency_lirun','c_agency_lirun','inviter_lirun','p_inviter_lirun'),
);
$id_case = array('fid'=>'','inviterID'=>'','P_inviterID'=>'','agencyID_C'=>'','agencyID_P'=>'',);
if(in_array($_SESSION['saccount'],$admin_account)){
    $id_case['admin_case']='admin_case';
    goto mark_1;
}
//查询是否是邀请人
$sel_inviterID = "select id from pft_member  where inviterID={$_SESSION['sid']}";
$GLOBALS['le']->query($sel_inviterID);
while($row[] = $GLOBALS['le']->fetch_assoc()){}
array_pop($row); 

if(count($row)){
    foreach($row as $v){
        $mids_1 .= $v['id'].',';
    }
}
$mids_1 = substr( $mids_1, 0, -1);

if($mids_1!=''){
    //查询被邀请人是否加入联盟的情况
    $sel_inviter_union = "select fid from pft_union_member_info_SE where memberID in ($mids_1)";
    $GLOBALS['le']->query($sel_inviter_union);
    unset($row);
    while($row[] = $GLOBALS['le']->fetch_assoc()){}
    array_pop($row); 

    if($count_1 = count($row)){
        foreach($row as $v){
            $fids[$v['fid']] = $v['fid'];
            $id_case['inviterID'][$v['fid']] = $v['fid'];
            if(!in_array($v['fid'],$member_ids))
                $member_ids[] = $v['fid'];
        }          
// echo "<pre>";
// print_r($id_case);
// print_r($fids);
// echo "</pre>";
// exit;        
        foreach($id_case['inviterID'] as $v){
                $tmp .= $v.',';
        }        
        $id_case['inviterID'] = substr( $tmp, 0, -1);
        unset($tmp);
    }
    
    //是否有下级被邀请人
    $sel_P_inviterID = "select id from pft_member where inviterID in($mids_1)";
    $GLOBALS['le']->query($sel_P_inviterID);
    unset($row);
    while($row[] = $GLOBALS['le']->fetch_assoc()){}
    array_pop($row); 

    foreach($row as $v){
        $mids_2 .= $v['id'].',';
    }
    $mids_2 = substr( $mids_2, 0, -1);

    //查询下级被邀请人是否加入联盟的情况
    if($mids_2!=''){
        $sel_p_inviter_union = "select fid from pft_union_member_info_SE where memberID in ($mids_2)";
        $GLOBALS['le']->query($sel_p_inviter_union);
        unset($row);
        while($row[] = $GLOBALS['le']->fetch_assoc()){}
        array_pop($row); 
        if($count_2 = count($row)){
            foreach($row as $v){
                $id_case['P_inviterID'][$v['fid']] = $v['fid'];
                $fids[$v['fid']] = $v['fid'];
                if(!in_array($v['fid'],$member_ids))
                    $member_ids[] = $v['fid'];
            }                 
            foreach($id_case['P_inviterID'] as $v){
                $tmp .= $v.',';
            }                   
            $id_case['P_inviterID']=substr( $tmp, 0, -1);
            unset($tmp);
        }
    }
}
//盟主
if($_SESSION['alliance']!='' ){
    $fids[$_SESSION['sid']] = $id_case['fid'] = $_SESSION['sid'];
    
}

//所有加入的联盟 不含盟主
$sel_union_fid = "SELECT fid from pft_union_member_info_SE where memberID in ({$_SESSION['sid']}) ";
$GLOBALS['le']->query($sel_union_fid);
unset($row);
while($row[] = $GLOBALS['le']->fetch_assoc()){
}
array_pop($row); 

if(count($row)){
    foreach($row as $v){
        $fids[$v['fid']] = $v['fid'];        
        $id_case['member'] .= $v['fid'].',';
    }
    $id_case['member'] = substr( $id_case['member'], 0, -1);
}

// exit; 

if(!count($fids) && !in_array($_SESSION['saccount'],$admin_account)){
    echo "<script>alert('您或您的邀请人还没有加入任何分销联盟');
                window.history.back(-1);     
            </script>";
	exit;
}

//判断是否代理商
$sel_agency = "SELECT fid,darea_code from pft_distributor_agency where inviterID={$_SESSION['sid']} and dstatus=0";
$GLOBALS['le']->query($sel_agency);
unset($row);
while($row[] = $GLOBALS['le']->fetch_assoc()){
}
array_pop($row);
if(count($row)){
    foreach($row as $v){
        if( strlen( $v['darea_code'] ) ==3){//省代理商情况
            $id_case['agencyID_P'] .= $v['fid'].',';
        }else{//市 直辖市代理商情况
            $id_case['agencyID_C'] .= $v['fid'].',';
        }
    }
   $id_case['agencyID_P'] = substr( $id_case['agencyID_P'], 0, -1); 
   $id_case['agencyID_C'] = substr( $id_case['agencyID_C'], 0, -1); 
}


// echo "<pre>";
// var_export($id_case);
// print_r($fids);
// echo "</pre>";

$agencyID_P_case = array(
    'C' => array('供应商','省代理商','市代理商','邀请人','分销商'),
    'T' => array('applyid','agencyID_P','agencyID_C','inviterID','P_inviterID'),
    'M' => array('apply_lirun','p_agency_lirun','c_agency_lirun','inviter_lirun','p_inviter_lirun'),
);

$agencyID_C_case = array(
    'C' => array('供应商','市代理商','邀请人','分销商'),
    'T' => array('applyid','agencyID_C','inviterID','P_inviterID'),
    'M' => array('apply_lirun','c_agency_lirun','inviter_lirun','p_inviter_lirun'),
);
foreach($fids as $v){
    $fid .= $v.',';
}
$fid = substr( $fid, 0, -1);
$where .= "  and fid in($fid)";
mark_1:
$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:5;
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$offset=($currentPage-1)*$pageSize;
// print_r($id_case);exit;
$sel_lirun = "select * from pft_distributor_union_lirun";
if($_GET['orderid']){
    $sel_lirun .=" where orderid={$_GET['orderid']} limit 1";
}else{
    if($_GET['beginDate'])
        $where .= " and rectime >'{$_GET['beginDate']}' ";
    if($_GET['endDate'])
        $where .= " and rectime <'{$_GET['endDate']}' ";
    $sel_lirun .= $where;
    $sel_lirun .= " order by id desc limit $offset,$pageSize";
}
$GLOBALS['le']->query($sel_lirun);
// echo $sel_lirun;
$row = array();
while($ra[]=$GLOBALS['le']->fetch_assoc()){
}
array_pop($ra);
// echo "<pre>";
// print_r($ra);
// exit;
// echo "</pre>";
$sel_count = "select count(*) from pft_distributor_union_lirun".$where;
$GLOBALS['le']->query($sel_count);
$GLOBALS['le']->fetch_assoc();
$count = $GLOBALS['le']->f('count(*)');
//core part
if(in_array($_SESSION['saccount'],$admin_account)){
    foreach($ra as $k=>$v){
        foreach($admin_case['T'] as $key => $val){
                $r[] = array(
                    'id' => $v[$val],//帐号
                    'rectime' => $v['rectime'],//时间
                    'orderid' => $v['orderid'],//订单号
                    'ID_case' => $admin_case['C'][$key],//联盟内的身份
                    'lmoney' => $v[$admin_case['M'][$key]],
                );
                if(!in_array($v[$val],$member_ids))
                    $member_ids[] = $v[$val]; 
        }
   }
}else{
    // print_r($id_case);
    // print_r($id_case['fid']);
    foreach($id_case as $k => $v){
        $id_case[$k] = explode(',',$v);
    }
    // print_r($id_case['fid']);
    // exit;
    foreach($ra as $k=>$v){
        if( in_array($v['fid'],$id_case['fid']) ){
            foreach($admin_case['T'] as $key => $val){
                $r[] = array(
                    'id' => $v[$val],//帐号
                    'rectime' => $v['rectime'],//时间
                    'orderid' => $v['orderid'],//订单号
                    'ID_case' => $admin_case['C'][$key],//联盟内的身份
                    'lmoney' => $v[$admin_case['M'][$key]],
                );
                 if(!in_array($v[$val],$member_ids))
                    $member_ids[] = $v[$val];
            }
        }else if( in_array($v['fid'],$id_case['agencyID_P']) ){
            foreach($agencyID_P_case['T'] as $key => $val){
                $r[] = array(
                    'id' => $v[$val],//帐号
                    'rectime' => $v['rectime'],//时间
                    'orderid' => $v['orderid'],//订单号
                    'ID_case' => $agencyID_P_case['C'][$key],//联盟内的身份
                    'lmoney' => $v[$agencyID_P_case['M'][$key]],
                );
                 if(!in_array($v[$val],$member_ids))
                    $member_ids[] = $v[$val];
            }
        }else if( in_array($v['fid'],$id_case['agencyID_C']) ){
            foreach($agencyID_C_case['T'] as $key => $val){
                $r[] = array(
                    'id' => $v[$val],//帐号
                    'rectime' => $v['rectime'],//时间
                    'orderid' => $v['orderid'],//订单号
                    'ID_case' => $agencyID_C_case['C'][$key],//联盟内的身份
                    'lmoney' => $v[$agencyID_C_case['M'][$key]],
                );
                 if(!in_array($v[$val],$member_ids))
                    $member_ids[] = $v[$val];
            }
        }else{
            if( in_array($v['fid'],$id_case['inviterID']) ){
                $r[] = array(
                    'id' => $v['inviterID'],//帐号
                    'rectime' => $v['rectime'],//时间
                    'orderid' => $v['orderid'],//订单号
                    'ID_case' => '邀请人',//联盟内的身份
                    'lmoney' => $v['inviter_lirun'],
                );
            }
            if( in_array($v['fid'],$id_case['P_inviterID']) ){
               $r[] = array(
                    'id' => $v['P_inviterID'],//帐号
                    'rectime' => $v['rectime'],//时间
                    'orderid' => $v['orderid'],//订单号
                    'ID_case' => '上级邀请人',//联盟内的身份
                    'lmoney' => $v['p_inviter_lirun'],
                );
            }
            if( $v['applyid']==$_SESSION['sid']){
                $r[] = array(
                    'id' => $v['applyid'],//帐号
                    'rectime' => $v['rectime'],//时间
                    'orderid' => $v['orderid'],//订单号
                    'ID_case' => '供应商',//联盟内的身份
                    'lmoney' => $v['apply_lirun'],
                );
            }
        }
    }
}


//查询出现的用户的名字
foreach($member_ids as $v){
    $implode_ids .= $v.',';
}
$implode_ids = substr($implode_ids,0,-1);
if($implode_ids!=''){
    $sel_dname = "select id,dname from pft_member where id in($implode_ids)";
    $GLOBALS['le']->query($sel_dname);
    $row = array();
    while($dd = $GLOBALS['le']->fetch_assoc()){
        $dname[$dd['id']] = $dd['dname'];
    }
}
$dname[$_SESSION['sid']] = $_SESSION['dname'];

include("tpl/distributor_union_lirun.tpl");
include("common/footer.html");
exit;
?>